$(function () {
    //验证手机号码格式
    var yzphone = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
    var phone = "";
    $("#phone").blur(function(){
       if(!yzphone.test($("#phone").val())){
           var layer = layui.layer
               ,form = layui.form;
           layer.msg('请输入正确的手机号码');
           return;
       }else{
           //获得手机号码
           phone = $("#phone").val();
           //注册用户名验证
            $.ajax({
                type: "post",
                url: "../user/yzPhone.json",
                data: {"phones": phone},
                success: function (result) {
                    if (result.defult == "true") {
                        var layer = layui.layer
                            , form = layui.form;
                        layer.msg('该号码已被占用');
                        return;
                    }
                }
            })
        }

    })
    $("#codeYz").blur(function () {
        if($("#codeYz").val()!=$code){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('请输入正确的验证码');
            return;
        }
    })

    //校验验证码
    // var $code = "";
    // $("#true").click(function(){
    //     var inputCode = document.getElementById("input").value; //取得输入的验证码
    //     if(inputCode.length <= 0) { //若输入的验证码长度为0
    //         // 则弹出请输入验证码
    //         var layer = layui.layer
    //             , form = layui.form;
    //         layer.msg('请输入验证码！');
    //         return;
    //     }else if(inputCode != code ) { //若输入的验证码与产生的验证码不一致时
    //         //alert("验证码输入错误！@_@");
    //         // 则弹出验证码输入错误
    //         var layer = layui.layer
    //             , form = layui.form;
    //         layer.msg('验证码输入错误！@_@');
    //         createCode();//刷新验证码
    //         document.getElementById("input").value = "";//清空文本框
    //     }else {
    //
    //         //判断验证码
    //         $("#codeYz").blur(function(){
    //             if($("#codeYz").val()!=$code){
    //                 var layer = layui.layer
    //                     , form = layui.form;
    //                 layer.msg('请输入正确的验证码');
    //                 return;
    //             }
    //         })
    //     }
    // })


    //密码长度验证
    $("#pwd").blur(function(){
        if($("#pwd").val().length<6){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('密码长度不能小于6位');
            return;
        } if($("#pwd").val().length>12){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('密码长度不能大于12位');
            return;
        } if(!$("#pwd").val().match(/[0-9]/)){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('密码不能为单一数字');
            return;
        } if(!$("#pwd").val().match(/[a-zA-Z]/)){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('密码不能为单一字母');
            return;
        }
    })
    //再次验证密码
    $("#qrpwd").blur(function(){
        if($("#qrpwd").val()!=$("#pwd").val()){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('两次密码输入不一致');
            return;
        }
    })

    //注册
    $("#register").click(function(){
        if(phone==""){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('手机号码不能为空');
            return;
        }
        if($("#pwd").val()==""){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('密码不能为空');
            return;
        }
        if($("#codeYz").val()!=$code){
            var layer = layui.layer
                , form = layui.form;
            layer.msg('请输入正确的验证码');
            return;
        }
        $.ajax({
            type:"post",
            url:"../user/register.json",
            data:{"phone":phone,"pwd":$("#pwd").val()},
            success:function(result){
               if(result.map=="true"){
                   var layer = layui.layer
                       , form = layui.form;
                   layer.msg('注册成功');
                    setTimeout(function(){
                        location.href="sign.jsp";
                    },2000);
               }else{
                   var layer = layui.layer
                       , form = layui.form;
                   layer.msg('注册失败');
                   setTimeout(function(){
                       location.href="register.jsp";
                   },2000);
               }
            }
        })
    })
})

